package core;

/**
 * Superclass of nucleotides.
 * Contains common code to all nucleotides.
 */
abstract public class Nucleotide {
    /** Possible letters **/
    public static final char[] letters = {'A', 'C', 'G', 'T'};
    
    protected String word;
    
    public Nucleotide(String word) throws Exception {
        // Checks the length of the word
        if(word.length() == 2) {
            // Checks if the letters are correct
            for(int i = 0; i < this.getWordLength(); i++) {
                boolean found = false;
                for(int j = 0; j < letters.length; j++) {
                    if(word.charAt(i) == letters[j]) {
                        found = true; break;
                    }
                }
                if(!found) {
                    throw new Exception("Erreur dans les lettres du nucléotide : "+word);
                }
            }
            this.word = word;
        }
    }
    
    /**
     * Returns the complementary of this nucleotide.
     * @return 
     */
    final public String getComplementary() {
        String res = "";
        for(int i = this.word.length(); i > 0; i--) {
            if(this.word.charAt(i) == 'A') { res += 'T'; }
            if(this.word.charAt(i) == 'C') { res += 'G'; }
            if(this.word.charAt(i) == 'G') { res += 'C'; }
            if(this.word.charAt(i) == 'T') { res += 'A'; }
        }
        return res;        
    }
    
    /**
     * Returns the letters of the nucleotide.
     * @return
     */
    public String getWord() {
        return this.word;
    }
    
    /**
     * Returns the mirror of the nucleotide.
     * @return 
     */
    public String getMirror() {
        String res = "";
        for(int i = this.word.length(); i > 0; i--) {
            res += this.word.charAt(i);
        }
        return res; 
    }
    
    @Override
    public boolean equals(Object obj) {
        if(this == obj) { return true; }
        if(!(obj instanceof Nucleotide)) { return false; }
        Nucleotide n = (Nucleotide) obj;
        return (this.word.equals(n.getWord()));
    }
    
    protected abstract int getWordLength();
}
